<?php

namespace Common\Model;

/**
 * 货币表
 * @modify wenhui 2018-01-30 创建
 */
class CurrencyHotelModel extends BaseModel {
    protected $trueTableName = 'tb_currency_hotel';
    protected $field = 'id,hotelentity_id,currency_id,type,isdelete';

    /**
     * 获取客栈默认货币
     * @modify wenhui 2018-01-30 创建
     * @return array
     */
    public function getDefaultCurrency($hotelId) {
        $option['hotelentity_id'] = $hotelId;
        $option['type'] = 1;
        $option['isdelete'] = NOT_DELETE;

        $data = $this->field($this->field)->where($option)->find();

        return empty($data) ? array() : $data;
    }

    /**
     * 获取客栈货币
     * @modify wenhui 2018-01-30 创建
     * @return array
     */
    public function getHotelCurrency($hotelId, $currencyId, $isDelete = false) {
        $option['hotelentity_id'] = $hotelId;
        $option['currency_id'] = $currencyId;

        if (!$isDelete) {
            $option['isdelete'] = NOT_DELETE;
        }

        $data = $this->field($this->field)->where($option)->find();

        return empty($data) ? array() : $data;
    }

    /**
     * 获取客栈货币
     * @modify wenhui 2018-01-30 创建
     * @return array
     */
    public function getHotelCurrencyByHotel($hotelId, $isDelete = false) {
        $option['hotelentity_id'] = $hotelId;

        if (!$isDelete) {
            $option['isdelete'] = NOT_DELETE;
        }

        $data = $this->field($this->field)->where($option)->select();

        return empty($data) ? array() : $data;
    }

    /**
     * 获取客栈货币
     * @modify wenhui 2018-01-30 创建
     * @param  integer   $hotelCurrencyId    客栈货币id
     * @return array
     */
    public function getInfoByHotelCurrencyId($hotelCurrencyId, $isDelete = false) {
        $option['id'] = $hotelCurrencyId;

        if (!$isDelete) {
            $option['isdelete'] = NOT_DELETE;
        }

        $data = $this->field($this->field)->where($option)->find();

        return empty($data) ? array() : $data;
    }

    /**
     * 添加条目
     * @modify wenhui 2018-01-30 创建
     * @param  integer   $hotelId    客栈id
     * @param  integer   $currencyId  货币id
     * @param  integer   $type  1=不可删除 0=可删除
     */
    public function addDefaultCurrency($hotelId, $currencyId, $type) {
        $data = array(
            'hotelentity_id' => $hotelId,
            'currency_id' => $currencyId,
            'type' => $type,
        );

        $result = $this->add($data);
        return empty($result) ? false : $result;
    }

    /**
     * 修改信息
     * @modify wenhui 2018-01-30 创建
     * @param  integer   $hotelCurrencyId    客栈货币id
     * @param  integer   $currencyId      货币id
     * @param  integer   $isDelete      0=启用1=删除
     */
    public function saveHotelCurrencyById($hotelCurrencyId, $currencyId = null, $isDelete = null) {
        $option = array(
            'id' => $hotelCurrencyId,
        );

        $data = array();
        if (!is_null($currencyId)) {
            $data['currency_id'] = $currencyId;
        }
        if (!is_null($isDelete)) {
            $data['isdelete'] = $isDelete;
        }
        

        return $this->where($option)->save($data) === false ? false : true;
    }

    /**
     * 修改信息
     * @modify wenhui 2018-01-30 创建
     * @param  integer   $hotelId    客栈id
     * @param  integer   $currencyId      货币id
     * @param  integer   $isDelete      0=启用1=删除
     */
    public function saveHotelCurrencyByHotelAndcuId($hotelId, $currencyId, $isDelete = null) {
        $option = array(
            'hotelentity_id' => $hotelId,
            'currency_id' => $currencyId,
        );

        $data = array();
        if (!is_null($isDelete)) {
            $data['isdelete'] = $isDelete;
        }

        return $this->where($option)->save($data) === false ? false : true;
    }
}
